/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans.validation;

import db.entities.Patient;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.TypedQuery;
import logger.Log;

/**
 *
 * @author marcy
 */
public class PasswordChangeBean {

    /**
     * Creates a new instance of passwordChangeBean
     */
    public PasswordChangeBean() {
    }
    private String newPassword;
    private int patientId;

    public int getPatientId() {
        return patientId;
    }

    public void setPatientId(int patientId) {
        this.patientId = patientId;
    }

    public String getNewPassword() {
        return newPassword;
    }

    public void setNewPassword(String newPassword) {
        this.newPassword = newPassword;
    }

    public String cambiaPassword() {
        //Ottengo un'istanza della Persistance Unit
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("PortaleVisitePU");
        EntityManager em = emf.createEntityManager();
        Patient p = null;

        //Creo la query
        TypedQuery<Patient> query = em.createNamedQuery("Patient.findById", Patient.class);
        try {
            p = query.setParameter("id", getPatientId()).getSingleResult();
            em.getTransaction().begin();
            p.setPassword(newPassword);
        } catch (NoResultException ex) {
            Log.getLogger().error("Problema con la query per il Cambio PASSWORD");
            return null;
        } finally {
            em.getTransaction().commit();
        }
        return "successo";

    }
}
